package com.czjtxy.extraTerm.daoImpl;

import com.czjtxy.extraTerm.dao.TdeptDao;
import com.czjtxy.extraTerm.entity.DO.Tdept;
import com.czjtxy.extraTerm.util.Util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangYing
 * @class 软件1903
 * @studentID 19852035
 * @date 2021/7/1
 */
public class TdeptDaoImpl extends BaseDaoImpl implements TdeptDao {
    @Override
    public List<Tdept> getall() throws SQLException, ClassNotFoundException {
//        打开数据库
        super.openDB();
//        sql语句
        String sql = "SELECT * FROM dept_inf";
//        预编译
        ps = con.prepareStatement(sql);
//        执行sql语句
        rs = ps.executeQuery();
//        判断
        List<Tdept> tdepts = new ArrayList<>();
        while (rs.next()) {
            tdepts.add(conventRsToTdept(rs));
        }
        return tdepts;
    }


    @Override
    public Tdept getOne(int id) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM dept_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id);
        rs = ps.executeQuery();
        Tdept tdept = null;
        if (rs.next()) {
            tdept = conventRsToTdept(rs);
        }
        return tdept;
    }

    @Override
    public void updateDept(Tdept tdept) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "UPDATE dept_inf SET `NAME` = ?, REMARK = ? WHERE ID = ?";
        ps = conventTdeptToPs(sql, tdept);
        ps.setInt(3, tdept.getId());
        ps.executeUpdate();
    }

    @Override
    public void deleteDept(int id) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "DELETE FROM dept_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    }

    @Override
    public void addDept(Tdept tdept) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "INSERT INTO dept_inf(NAME, REMARK) VALUES(?, ?)";
        ps = conventTdeptToPs(sql, tdept);
        ps.executeUpdate();
    }

    @Override
    public List<Tdept> getPart(String name) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM dept_inf WHERE `NAME` LIKE ?";
        ps = con.prepareStatement(sql);
        ps.setString(1, Util.likeStr(name));
        rs = ps.executeQuery();
        List<Tdept> tdepts = new ArrayList<>();
        while (rs.next()) {
            tdepts.add(conventRsToTdept(rs));
        }
        return tdepts;
    }

    private Tdept conventRsToTdept(ResultSet rs) throws SQLException {
        Tdept tdept = new Tdept();
        tdept.setId(rs.getInt("id"));
        tdept.setName(rs.getString("name"));
        tdept.setRemark(rs.getString("remark"));
        return tdept;
    }

    private PreparedStatement conventTdeptToPs(String sql, Tdept tdept) throws SQLException {
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, tdept.getName());
        ps.setString(2, tdept.getRemark());
        return ps;
    }
}
